package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.OrderPageDto;
import com.hoongs.v1.entity.Order;
import com.hoongs.v1.service.OrderService;
import com.hoongs.v1.utils.Result;
import com.hoongs.v1.vo.OrderVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/order")
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;


    @GetMapping("/selectListByUser")
    public Result selectListByUser(@RequestHeader("token") String token) {
        return orderService.selectListByUser(token);
    }


    @PostMapping("/addByUser")
    public Result addByUser(@Validated OrderVo orderVo, BindingResult bindingResult, @RequestHeader("token") String token) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : orderService.addByUser(orderVo, token);
    }

    @PostMapping("/changeRider")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result changeRider(String riderId, String orderId) {
        return orderService.changeRider(riderId, orderId);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Order order) {
        return orderService.add(order);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return orderService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Order order) {
        return orderService.update(order);
    }

    @GetMapping("/selectById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectById(String id) {
        return orderService.selectById(id);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(OrderPageDto orderDto) {
        return orderService.selectPageList(orderDto);
    }

    @PostMapping("/done")
    public Result done(String id) {
        return orderService.done(id);
    }

    @PostMapping("/userCancel")
    public Result userCancel(String orderId, @RequestHeader("token") String token) {
        return orderService.userCancel(orderId, token);
    }

    @PostMapping("/cancel")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result cancel(String id, String text) {
        return orderService.cancel(id, text);
    }

}
